/*
 * TimeFormat.java
 *
 * Created on 23 February 2006, 13:22
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package web.util;

//import business.util.Time;
import se392.ateam2006.meetingmgmt.meeting.Duration;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;


/**
 *
 * @author Chris
 */
public class TimeFormat {

    private static final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    
    public static Duration parseTime(String s) throws Exception{
        int bp = 0;
        return parseTime(s, Duration.class);
    }
    
    public static Duration parseTime(String time, Class type) throws Exception{
        int days=0;
        int hours=0;
        int minutes=0;
        int e;
        if(Pattern.matches("\\d+ \\d+:\\d+",  time)){
            e = time.indexOf(" ");
            days = Integer.parseInt(time.substring(0,e));
            time = time.substring(e+1);            
        }
        if(Pattern.matches("\\d+:\\d+",  time)){
            e = time.indexOf(":");
            hours = Integer.parseInt(time.substring(0, e));
            minutes = Integer.parseInt(time.substring(e+1));
        }
        return new Duration(days, hours, minutes);
    }
    
    public static Date parseDate(String date) throws Exception {
        return (Date) dateFormat.parse(date);
    }
}
